# 安装pycrypto  pip install pycryptodome
import json
from base64 import b64encode

import requests
from Crypto.Cipher import AES

# proxies = {
#     "http": "http://114.231.82.246:8089"
# }
#
# res = requests.get("https://www.baidu.com", proxies=proxies)
# res.encoding = 'utf-8'
# print(res.text)


url = "https://music.163.com/weapi/comment/resource/comments/get"
header = {
    "referer": "https://music.163.com/",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0"
}
# data = {
#     "params": "EyTrmkJMauNB1DGfOtXdGO+aSUT8neN4qnH4zStP0VSHBcyKKsEuzWWou1segwhC/4ccCkNFy34eRjmaF3EIkiUYZvI85HGXsRRvmCwNL2szGOwY7l5ZzeCOgWdfCP1E73kFTHm0oUPLPfnX9DQ+Mg8g7nwp7+0IRR5t7QvWc8+snAA8rvdFt8Yr7TvrahJX2mhoKZh0coOSSmnWSHapZlFIOx2gq9ZBsyJc1GeLpL1I/eTk9KFuMWWtSi7JGKbT+WKsQ5xJvJEaRnmaN/n2YIw/rQF16XUYcG9ud6rWHS4=",
#     "encSecKey": "18d3752047fbc349d33c5efaa4dbca2cf1905abfef249b7e0321e4a81341a602adec866e0b8d7f6efca87d4a781cdadc050d020f31e3ab008e4deb04c239c2c935e2f8af90146629dca5820113645ef83f6201802fedad4338954aec61ed7215302667088d6c5346a9d2573ea57cf2b54838c44517cff8c8dd66cf33d8520c7c"
# }


# 处理加密过程
"""
    function a(a) {
        var d, e, b = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", c = "";
        for (d = 0; a > d; d += 1)
            e = Math.random() * b.length,
            e = Math.floor(e),  # 取整
            c += b.charAt(e);   # 取出b中第e个位置的数据
        return c
    }
    function b(a, b) { # a加密内容 b密钥
        var c = CryptoJS.enc.Utf8.parse(b)
          , d = CryptoJS.enc.Utf8.parse("0102030405060708")
          , e = CryptoJS.enc.Utf8.parse(a)
          , f = CryptoJS.AES.encrypt(e, c, {    # e加密内容 c加密密钥
            iv: d,                      # 在AES加密算法中为偏移量
            mode: CryptoJS.mode.CBC     # CBC加密模式
        });
        return f.toString()
    }
    function c(a, b, c) {
        var d, e;
        return setMaxDigits(131),
        d = new RSAKeyPair(b,"",c),
        e = encryptedString(d, a)
    }
    # 参数data
    # d:数据  
    # e:定值：bse8W(["流泪", "强"])=010001 
    # f:定值：bse8W(RR7K.md)=00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7
    # g:定值：bse8W(["爱心", "女孩", "惊恐", "大笑"])=0CoJUm6Qyw8W8jud
    function d(d, e, f, g) {
        var h = {}
          , i = a(16);  # 获得16个随机字母或数字
        return h.encText = b(d, g),
        h.encText = b(h.encText, i),    # params
        h.encSecKey = c(i, e, f),       # encSecKey i可以设一个定值
        h
    }
    function e(a, b, d, e) {
        var f = {};
        return f.encText = c(a + e, b, d),
        f
    }
    window.asrsea = d,
    
    window.asrsea(JSON.stringify(i6c), bse8W(["流泪", "强"]), bse8W(RR7K.md), bse8W(["爱心", "女孩", "惊恐", "大笑"]));
"""

data = {
    "rid": "R_SO_4_1303464858",
    "threadId": "R_SO_4_1303464858",
    "pageNo": "1",
    "pageSize": "20",
    "cursor": "-1",
    "offset": "0",
    "orderType": "1",
    "csrf_token": ""
}
e = "010001"
f = "00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7"
g = "0CoJUm6Qyw8W8jud"
i = "UJ3PQ30Q1lNXzype"


# 通过debug一次i的结果得出
def get_encSecKey():
    return "cd0496672351f29874faef4e2c10b336a3001e76380047b1c7935a73073debecd20e33d34bb9751b0a41e782f06462da090245cf14697c4baa735abcd3c0c79e2ac0af16cd15a0f97fae749888254f4e2fd6b3a1640da97b974512c259cb819f65823af9095c8bb03a21a72ca460e13c56ad0830611f76e4735308e87c51ab04"


def get_params(data):
    first = enc_params(data, g)
    second = enc_params(first, i)
    return second

def to_16(data):
    pad = 16- len(data) % 16
    data += chr(pad) * pad
    return data

def enc_params(data, key):
    """
    还原b方法的AES加密过程
    :param data: 加密数据
    :param key:  加密key
    :return:
    """
    iv = "0102030405060708"
    aes = AES.new(key=key.encode("utf-8"), iv=iv.encode("utf-8"), mode=AES.MODE_CBC)  # 创建加密器
    bs = aes.encrypt(to_16(data).encode("utf-8"))  # 加密 得到字节;加密内容的长度必须是16的倍数
    return str(b64encode(bs), "utf-8")  # 转换为字符串返回


res = requests.post(url, data={
    "params": get_params(json.dumps(data)),
    "encSecKey": get_encSecKey()
})
print(res.text)